package gblend.formatcovert;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
* Copyright 2010, Center for Advanced Information Systems, Nanyang Technological University
*
* File name: AIDStoQuickSI.java
*
* Abstract: Convert AIDS file to QuickSI format
*
* Current Version:      0.1
* Auther:               Jin Changjiu
* Modified Date:        June.3,2009
*
*/
/**
 *
 * @author cjjin
 */

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Vector;

public class AIDStoQuickSI {

    private static Vector<Integer> h = new Vector<Integer>();
    private static Vector<String> label = new Vector<String>();
    public static void main (String[] args) throws FileNotFoundException
    {
         String filename = "AIDO99SD";
        formatDotFile(filename);
    }

    public static int graphInclude(String nodelabel)
    {
      for(int i = 0; i < label.size(); i++)
      {
        String str = label.elementAt(i);
        if(str.equals(nodelabel))
        {
           return i;
        }
      }
      return -1;//didn't find

    }


    public static void formatDotFile(String filename)
    {
	String output1="V.txt", output2 = "E.txt",output3="AIDS";
        String strLine = null;
	FileOutputStream outStream1,outStream2,outStream3;
	PrintStream p;
        PrintStream q;
        PrintStream a;
	FileInputStream inStream;
	DataInputStream in;
	BufferedReader br;

	File fin=new File(filename);
	File fout1=new File(output1);
        File fout2=new File(output2);
        File fout3=new File(output3);
	if(fin.exists())
	{
          try {

               	outStream1=new FileOutputStream(fout1,false);
                outStream2=new FileOutputStream(fout2,false);
                outStream3=new FileOutputStream(fout3,false);
		inStream=new FileInputStream(fin);
		// Connect print stream to the output stream
                p = new PrintStream( outStream1 );
                q = new PrintStream( outStream2 );
                a = new PrintStream( outStream3 );
		// Get the object of DataInputStream
                in = new DataInputStream(inStream);
                br = new BufferedReader(new InputStreamReader(in));
                //Read file line by line
                try {
                        int nodenum = 0;
                        int edgenum = 0;
                        int graphnum = 0;
                        int src = 0;
                        int trg = 0;
                        while ((strLine = br.readLine()) != null)
                        {
                            if(graphnum<20000)
                            {
                           	// Print the content on the console
                            if(strLine.contains("Corina"))
                            {
                               strLine = br.readLine();
                               nodenum = Integer.parseInt(strLine.substring(0,3).trim());
                               edgenum = Integer.parseInt(strLine.substring(3,6).trim());
                               String[] nodeline = new String[nodenum];
                               
                               int l = 0;
                               for(int m = 0; m < nodenum; m++)
                               {
                                    strLine = br.readLine();
                                    strLine = strLine.substring(31,33).trim();

                                    if(strLine.equals("H"))
                                    {
                                        h.addElement(m);
                                    }
                                    else
                                    {
                                        String newlabel = null;
                                        int flag = graphInclude(strLine);
                                        if(flag==-1)
                                        {
                                            newlabel=""+label.size();
                                            label.addElement(strLine);

                                        }
                                        else
                                        {
                                            newlabel =""+flag;
                                        }
                                        //maybe H in the middle 5.16
                                        nodeline[l++]="v "+(m-h.size())+" v"+newlabel;

                                    }
                                }
                                int newnodenum = nodenum-h.size();
                             
                                Vector<String> astr = new Vector<String>();
                                for(int n = 0; n < edgenum; n++)
                                {
                                      boolean f = false;
                                      strLine = br.readLine();
                                      src = Integer.parseInt(strLine.substring(0,3).trim())-1;
                                      trg = Integer.parseInt(strLine.substring(3,6).trim())-1;

                                      for(int t = 0;t<h.size();t++)
                                      {
                                        if(src==h.elementAt(t)||trg ==h.elementAt(t))
                                        {
                                                  f = true;
                                                  break;
                                        }
                                      }
                                      if(f == false)
                                      {
                                          if(src<trg)
                                          {
                                              for(int t = h.size()-1;t>=0;t--)
                                              {
                                                if(src>h.elementAt(t))
                                                {
                                                    src=src-t-1;
                                                    break;
                                                }
                                              }

                                              for(int t = h.size()-1;t>=0;t--)
                                              {
                                                 if(trg >h.elementAt(t))
                                                 {
                                                    trg=trg-t-1;
                                                    break;
                                                 }
                                              }

                                              astr.addElement("u "+src+" "+trg+" e0");
                          
                                          }
                                      }
                                }

                                h.clear();
            
                                if(astr.size()>=(newnodenum-1)&&(newnodenum>1))
                                {
                                  a.println("t # "+graphnum+" "+newnodenum);
                                  for(int m = 0; m<newnodenum; m++)
                                  {
                                        a.println(nodeline[m]);
                                  }


                                  for(int i = 0;i <astr.size();i++)
                                  {
                                    a.println(astr.elementAt(i));
                                  }

                                  graphnum++;
                                }
                            }//cor
                          }//end if num
			}
        		//Close the input stream
                        in.close();
	                a.close();
		}
                catch (IOException e)
                {
                        // TODO Auto-generated catch block
                	e.printStackTrace();
                }
		} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		}
            }
	}

}